package dao

import (
	"ssims/models"
)

func GetAllRelation(relation map[string]interface{}) (error, []models.Relation, int64) {
	var relationData []models.Relation
	page := relation["page"].(int)
	pageSize := relation["limit"].(int)
	searchId := relation["searchId"].(string)
	var total int64
	err := db.Table("relation").Select("family_id1", "id1_name", "family_id2", "id2_name", "relation_type").Where("family_id2 like ? and is_del = false", searchId+"%").Order("family_id1 ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&relationData).Error
	return err, relationData, total
}

func DelRelation(id string) error {
	err := db.Table("relation").Where("family_id1 = ?", id).Update("is_del", true).Error
	return err
}

func UpdateRelation(relation models.Relation) error {
	err := db.Table("relation").Select("family_id1", "id1_name", "family_id2", "id2_name", "relation_type").Where("family_id2 = ?", relation.FamilyId2).Updates(&relation).Error
	return err
}

func AddRelation(relation models.Relation) error {
	err := db.Table("relation").Select("family_id1", "id1_name", "family_id2", "id2_name", "relation_type", "is_del").Create(&relation).Error
	return err
}
